//
//  AppDelegate.m
//  QRNDemo
//
//  Created by 殷文昭 on 2017/2/27.
//  Copyright © 2017年 Qunar. All rights reserved.
//

#import "AppDelegate.h"


// QRN .h 文件
#import "QRCTVCCreater.h"
#import "QRCTConfigManager.h"
#import "QRCTVCController.h"


// QRN 分享相关 h文件
#import "QRCTSocialShare.h"
#import "QRCTSocialShareInstance.h"


// 独立 APP 相关信息
NSString *const AppVid = @"80001666";
NSString *const AppPid = @"66666";


@interface AppDelegate () <QRCTConfigManagerDelegate>

@property (nonatomic, strong) UINavigationController *navi;

@end


@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // 设置分享key
    [QRCTSocialShareInstance registerWeiBoID:@"2650284251"];
    [QRCTSocialShareInstance registerWeChatID:@"wxd3fe931c8c4dfef9"];
    [QRCTSocialShareInstance registerQQID:@"1103194207"];
    
    // QRN 相关配置类
    [QRCTConfigManager setDelegate:self];
    
    
    UIViewController *rootViewController = [[UIViewController alloc] init];
    
    UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [window setBackgroundColor:[UIColor clearColor]];
    [self setWindow:window];
    [window makeKeyAndVisible];
    
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    view.frame = [UIScreen mainScreen].bounds;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"点击加载 QRN 页面" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(createQRNVC) forControlEvents:UIControlEventTouchUpInside];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    button.frame = [UIScreen mainScreen].bounds;
    [view addSubview:button];
    rootViewController.view = view;
    
    _navi = [[UINavigationController alloc] initWithRootViewController:rootViewController];
    // 不使用 setNavigationBarHidden 方法，防止返回手势失效
    _navi.navigationBar.hidden = YES;
    [_window setRootViewController:_navi];
    [QRCTVCController setupNavigationController:_navi];
    
    return YES;
}


- (void) createQRNVC {
    
    [QRCTVCCreater createVCWithHybridId:@"qrn_demo"
                             moduleName:@"naive"
                      initialProperties:nil
                             hasLoading:YES
                      completionHandler:^(QRCTViewController *qrctVC, NSError *error) {
                          if (!qrctVC) {
                              return;
                          }
                          [QRCTVCController pushVC:qrctVC];
                      }];
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return [QRCTSocialShare handleURL:url];
}

#pragma mark - QRCTConfigManagerDelegate

// QRN  DeviceInfo  获取的信息
- (NSDictionary<NSString*, NSString*> *)appInfos{
    return @{
             @"vid": AppVid,
             @"pid": AppPid,
             @"cid": @"",
             @"uid": @"",
             @"sid": @"",
             @"gid": @"",
             @"scheme": @"",
             };
}

// hybridId 对应线上资源的 URL
- (NSURL *)bundleURL:(NSString *)hybridId{
    static NSURL *bizUrl;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        bizUrl = [[NSBundle mainBundle] URLForResource:@"biz.qrn_demo.ios" withExtension:@"bundle"];
    });
    
    return bizUrl;
}

// hybridId 对应调试工具中 beta 的 URL
- (NSURL *)betaBundleURL:(NSString *)hybridId branch:(NSString *)branch{
    
    branch = [branch stringByReplacingOccurrencesOfString:@"_" withString:@"%5F"];
    
    return [NSURL URLWithString:[NSString stringWithFormat:@"http://rn.beta.qunar.com/packages/%@_ios_beta_%@/index.bundle?vid=%@&pid=%@", hybridId, branch, AppVid, AppPid]];
}


// RN 异常处理方法，主要是 JS 异常
- (void) handQRCTFatal:(NSError *)error hybridId:(NSString *)hybridId{
    // TODO:
};


@end
